package day9;
//synchronized修饰代码块
public class Demo3 {
    public static synchronized void printA(){
        String name=Thread.currentThread().getName();
        System.out.println(name+"开始执行A方法");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name+"结束执行A方法");
    }
    public static void main(String[] args) {
        Object o=new Object();
        new Thread(()->{
            Demo3.printA();
        },"aaa").start();
        new Thread(()->{
            //同步代码块:锁的对象就是参数定义的对象
            synchronized (Demo3.class){
                String name=Thread.currentThread().getName();
                System.out.println(name+"开始执行代码块");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name+"结束执行代码块");
            }
        },"bbb").start();

        StringBuffer sb=new StringBuffer();
        new Thread(()-> sb.length() ,"sss").start();
        new Thread(()-> sb.charAt(0) ,"bbb").start();

    }
}
